/*     */ package com.zimbra.cs.session;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapTransport.DebugListener;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.zclient.ZClientException;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.io.PrintStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URI;
/*     */ import java.net.URISyntaxException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ import org.apache.commons.cli.PosixParser;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WaitSetValidator
/*     */   implements SoapTransport.DebugListener
/*     */ {
/*     */   private long mSendStart;
/*     */   private int mTimeout;
/*     */   private int mRetryCount;
/*     */   private SoapHttpTransport mTransport;
/*     */   private ZAuthToken mAuthToken;
/*     */   private long mAuthTokenLifetime;
/*     */   private long mAuthTokenExpiration;
/*     */   private SoapTransport.DebugListener mDebugListener;
/*     */   
/*     */   public WaitSetValidator()
/*     */   {
/*  54 */     this.mTimeout = -1;
/*  55 */     this.mRetryCount = -1;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  60 */     this.mDebugListener = null;
/*     */   }
/*     */   
/*  63 */   void setVerbose(boolean value) { if (value) {
/*  64 */       this.mDebugListener = this;
/*     */     } else {
/*  66 */       this.mDebugListener = null;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   void soapSetURI(String uri)
/*     */   {
/*  74 */     if (this.mTransport != null) this.mTransport.shutdown();
/*  75 */     this.mTransport = new SoapHttpTransport(uri);
/*  76 */     if (this.mTimeout >= 0)
/*  77 */       this.mTransport.setTimeout(this.mTimeout);
/*  78 */     if (this.mRetryCount > 0)
/*  79 */       this.mTransport.setRetryCount(this.mRetryCount);
/*  80 */     if (this.mAuthToken != null)
/*  81 */       this.mTransport.setAuthToken(this.mAuthToken);
/*  82 */     if (this.mDebugListener != null)
/*  83 */       this.mTransport.setDebugListener(this.mDebugListener);
/*     */   }
/*     */   
/*     */   private String serverName() {
/*     */     try {
/*  88 */       return new URI(this.mTransport.getURI()).getHost();
/*     */     } catch (URISyntaxException e) {}
/*  90 */     return this.mTransport.getURI();
/*     */   }
/*     */   
/*     */   private void checkTransport() throws ServiceException {
/*  94 */     if (this.mTransport == null)
/*  95 */       throw ServiceException.FAILURE("transport has not been initialized", null);
/*     */   }
/*     */   
/*     */   protected Element invoke(Element request) throws ServiceException {
/*  99 */     checkTransport();
/*     */     try
/*     */     {
/* 102 */       return this.mTransport.invoke(request);
/*     */     } catch (SoapFaultException e) {
/* 104 */       throw e;
/*     */     } catch (IOException e) {
/* 106 */       throw ZClientException.IO_ERROR("invoke " + e.getMessage() + ", server: " + serverName(), e);
/*     */     }
/*     */   }
/*     */   
/*     */   protected Element invokeOnTargetAccount(Element request, String targetId) throws ServiceException {
/* 111 */     checkTransport();
/*     */     
/* 113 */     String oldTarget = this.mTransport.getTargetAcctId();
/*     */     try {
/* 115 */       this.mTransport.setTargetAcctId(targetId);
/* 116 */       return this.mTransport.invoke(request);
/*     */     } catch (SoapFaultException e) {
/* 118 */       throw e;
/*     */     } catch (IOException e) {
/* 120 */       throw ZClientException.IO_ERROR("invoke " + e.getMessage() + ", server: " + serverName(), e);
/*     */     } finally {
/* 122 */       this.mTransport.setTargetAcctId(oldTarget);
/*     */     }
/*     */   }
/*     */   
/*     */   protected Element invokeQueryWaitSet(String id) throws ServiceException {
/* 127 */     Element.XMLElement req = new Element.XMLElement(AdminConstants.QUERY_WAIT_SET_REQUEST);
/* 128 */     req.addAttribute("waitSet", id);
/* 129 */     Element response = invoke(req);
/* 130 */     return response;
/*     */   }
/*     */   
/*     */   protected List<WaitSetSession> queryWaitSet(String id) throws ServiceException {
/* 134 */     Element qws = invokeQueryWaitSet(id);
/*     */     
/* 136 */     List<WaitSetSession> toRet = new ArrayList();
/*     */     
/* 138 */     for (Iterator<Element> iter = qws.elementIterator("session"); iter.hasNext();) {
/* 139 */       Element selt = (Element)iter.next();
/* 140 */       WaitSetSession wss = new WaitSetSession();
/* 141 */       wss.accountId = selt.getAttribute("account");
/* 142 */       wss.token = selt.getAttribute("token", null);
/* 143 */       toRet.add(wss);
/*     */     }
/* 145 */     return toRet;
/*     */   }
/*     */   
/*     */   protected boolean validateSessionStatus(WaitSetSession wss) throws ServiceException {
/* 149 */     Element.XMLElement req = new Element.XMLElement(MailConstants.SYNC_REQUEST);
/* 150 */     req.addAttribute("token", wss.token);
/* 151 */     Element syncResponse = invokeOnTargetAccount(req, wss.accountId);
/* 152 */     String newToken = syncResponse.getAttribute("token");
/* 153 */     String md = syncResponse.getAttribute("md");
/*     */     
/* 155 */     long diff = Long.parseLong(newToken) - Long.parseLong(wss.token);
/* 156 */     if ((!newToken.equals(wss.token)) && (diff > 10L)) {
/* 157 */       System.out.println("ERROR: Account " + wss.accountId + " -- old token " + wss.token + " doesn't match new token: " + newToken + " md=" + md + " Difference=" + diff);
/*     */       
/* 159 */       return false;
/*     */     }
/* 161 */     System.out.println("Account " + wss.accountId + " diff=" + diff + " -- OK");
/* 162 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void soapAdminAuthenticate(String name, String password)
/*     */     throws ServiceException
/*     */   {
/* 183 */     if (this.mTransport == null) throw ZClientException.CLIENT_ERROR("must call setURI before calling adminAuthenticate", null);
/* 184 */     Element.XMLElement req = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
/* 185 */     req.addElement("name").setText(name);
/* 186 */     req.addElement("password").setText(password);
/* 187 */     Element response = invoke(req);
/* 188 */     this.mAuthToken = new ZAuthToken(response.getElement("authToken"), true);
/* 189 */     this.mAuthTokenLifetime = response.getAttributeLong("lifetime");
/* 190 */     this.mAuthTokenExpiration = (System.currentTimeMillis() + this.mAuthTokenLifetime);
/* 191 */     this.mTransport.setAuthToken(this.mAuthToken);
/*     */   }
/*     */   
/*     */   public void receiveSoapMessage(Element envelope)
/*     */   {
/* 196 */     long end = System.currentTimeMillis();
/* 197 */     System.out.printf("======== SOAP RECEIVE =========\n", new Object[0]);
/* 198 */     System.out.println(envelope.prettyPrint());
/* 199 */     System.out.printf("=============================== (%d msecs)\n", new Object[] { Long.valueOf(end - this.mSendStart) });
/*     */   }
/*     */   
/*     */   public void sendSoapMessage(Element envelope)
/*     */   {
/* 204 */     this.mSendStart = System.currentTimeMillis();
/* 205 */     System.out.println("========== SOAP SEND ==========");
/* 206 */     System.out.println(envelope.prettyPrint());
/* 207 */     System.out.println("===============================");
/*     */   }
/*     */   
/*     */   void usage() {
/* 211 */     System.out.println("");
/* 212 */     System.out.println("testwaitset -i waitsetid [-u admin_user] [-p password] [-h host]");
/* 213 */     System.exit(1);
/*     */   }
/*     */   
/*     */   private static void printError(String text) {
/* 217 */     PrintStream ps = System.err;
/*     */     try {
/* 219 */       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ps, "UTF-8"));
/* 220 */       writer.write(text + "\n");
/* 221 */       writer.flush();
/*     */     } catch (UnsupportedEncodingException e) {
/* 223 */       ps.println(text);
/*     */     } catch (IOException e) {
/* 225 */       ps.println(text);
/*     */     }
/*     */   }
/*     */   
/*     */   void run(String id, String host, String user, String pw) {
/*     */     try {
/* 231 */       soapSetURI(host);
/* 232 */       soapAdminAuthenticate(user, pw);
/* 233 */       List<WaitSetSession> wss = queryWaitSet(id);
/* 234 */       boolean hasFailures = false;
/* 235 */       for (WaitSetSession w : wss) {
/* 236 */         if (!validateSessionStatus(w))
/* 237 */           hasFailures = true;
/*     */       }
/* 239 */       if (hasFailures) {
/* 240 */         Element qws = invokeQueryWaitSet(id);
/* 241 */         System.out.println(qws.prettyPrint());
/*     */       }
/*     */     }
/*     */     catch (ServiceException e) {
/* 245 */       e.printStackTrace();
/* 246 */       System.out.println("Caught exception: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 254 */     CliUtil.toolSetup();
/*     */     
/* 256 */     WaitSetValidator t = new WaitSetValidator();
/*     */     
/* 258 */     CommandLineParser parser = new PosixParser();
/* 259 */     Options options = new Options();
/*     */     
/* 261 */     options.addOption("i", "id", true, "Wait Set ID");
/* 262 */     options.addOption("h", "host", true, "Hostname");
/* 263 */     options.addOption("u", "user", true, "Username");
/* 264 */     options.addOption("p", "pw", true, "Password");
/* 265 */     options.addOption("?", "help", false, "Help");
/* 266 */     options.addOption("v", "verbose", false, "Verbose");
/*     */     
/* 268 */     CommandLine cl = null;
/* 269 */     boolean err = false;
/*     */     
/*     */ 
/*     */ 
/*     */     try
/*     */     {
/* 275 */       cl = parser.parse(options, args, true);
/*     */     } catch (ParseException pe) {
/* 277 */       printError("error: " + pe.getMessage());
/* 278 */       err = true;
/*     */     }
/*     */     
/* 281 */     if ((err) || (cl.hasOption('?'))) {
/* 282 */       t.usage();
/*     */     }
/*     */     
/* 285 */     String id = null;String host = null;String user = null;String pw = null;
/*     */     
/* 287 */     if (!cl.hasOption('i')) {
/* 288 */       t.usage();
/*     */     }
/* 290 */     id = cl.getOptionValue('i');
/*     */     
/* 292 */     if (cl.hasOption('h')) {
/* 293 */       host = cl.getOptionValue('h');
/*     */     } else {
/* 295 */       host = "http://localhost:7071/service/admin";
/*     */     }
/* 297 */     if (cl.hasOption('u')) {
/* 298 */       user = cl.getOptionValue('u');
/*     */     } else {
/* 300 */       user = "admin";
/*     */     }
/* 302 */     if (cl.hasOption('p')) {
/* 303 */       pw = cl.getOptionValue('p');
/*     */     } else {
/* 305 */       pw = "test123";
/*     */     }
/* 307 */     if (cl.hasOption('v')) {
/* 308 */       t.setVerbose(true);
/*     */     }
/* 310 */     String[] hosts = host.split(",");
/* 311 */     String[] ids = id.split(",");
/*     */     
/* 313 */     if (hosts.length != ids.length) {
/* 314 */       System.err.println("If multiple hosts or ids are specified, the same number is required of each");
/* 315 */       System.exit(3);
/*     */     }
/*     */     
/* 318 */     for (int i = 0; i < hosts.length; i++) {
/* 319 */       if (i > 0)
/* 320 */         System.out.println("\n\n");
/* 321 */       System.out.println("Checking server " + hosts[i] + " waitsetId=" + ids[i]);
/* 322 */       t.run(ids[i], hosts[i], user, pw);
/*     */     }
/*     */   }
/*     */   
/*     */   static class WaitSetSession
/*     */   {
/*     */     public String accountId;
/*     */     public String token;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/session/WaitSetValidator.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */